package dk.eclipse.android.badassapp.contact;

import java.io.InputStream;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;

/**
 * This helper class handles access to contact information.
 * 
 * @author Ronni Kahalani (eclipse.dk)
 * 
 */
public class ContactsHelper {

	/**
	 * The query projection used to fetch contact phone number, display name and
	 * photo id.
	 */
	private static String[] CONTACT_PROJECTION_BY_ID = {
			ContactsContract.CommonDataKinds.Phone.NUMBER,
			ContactsContract.Contacts.DISPLAY_NAME,
			ContactsContract.Contacts.PHOTO_ID,
			/* Need the lookupkey as handle and system wide uri to the contact */
			ContactsContract.Contacts.LOOKUP_KEY };

	/**
	 * Starts the standard contact picker, via an intent.
	 * 
	 * @param activity
	 * @param requestCode
	 */
	public static void startContactPicker(Activity activity, int requestCode) {

		Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
				ContactsContract.Contacts.CONTENT_URI);
		activity.startActivityForResult(contactPickerIntent, requestCode);
	}

	/**
	 * Finds a contact by contact id, and return a cursor to the contact.
	 * 
	 * @param activity
	 * @param contactId
	 * @return a cursor to the contact.
	 */
	public static Cursor getContactCursor(Activity activity, Long contactId) {

		final Cursor contact = activity.getContentResolver().query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
				CONTACT_PROJECTION_BY_ID,
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
				new String[] { contactId.toString() },
				ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY
						+ " DESC");

		return contact;
	}

	/**
	 * Locates a contact photo and returns a bitmap.
	 * 
	 * @param contentResolver
	 * @param contactId
	 * @return a contact photo bitmap.
	 */
	public static Bitmap getPhoto(ContentResolver contentResolver,
			Long contactId) {

		Uri contactPhotoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
				contactId);
		InputStream photoDataStream = Contacts.openContactPhotoInputStream(
				contentResolver, contactPhotoUri); // <-- always null
		Bitmap photo = BitmapFactory.decodeStream(photoDataStream);
		return photo;
	}

	/**
	 * @param cursor
	 * @return
	 */
	public static Long getContactId(Cursor cursor) {
		return cursor.getLong(cursor
				.getColumnIndex(ContactsContract.Contacts._ID));
	}

	/**
	 * @param cursor
	 * @return
	 */
	public static String getContactDisplayName(Cursor cursor) {
		return cursor.getString(cursor
				.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

	}

	/**
	 * @param cursor
	 * @return
	 */
	public static String getContactPhoneNumber(Cursor cursor) {
		return cursor.getString(cursor
				.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

	}

	/**
	 * @param cursor
	 * @return
	 */
	public static String getContactLookupKey(Cursor cursor) {
		return cursor.getString(cursor
				.getColumnIndexOrThrow(ContactsContract.Contacts.LOOKUP_KEY));
	}

}
